﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.IO;

namespace BluetoothMachine.ExpandHelper
{
    public class FileHelper
    {
        /// <summary>
        /// 检测文件是否存在，不存在返回NULL，存在则读取文件并删除文件返回
        /// </summary>
        /// <param name="time">时间内读取，毫秒为单位</param>
        /// <param name="path">读取文件路径</param>
        /// <param name="filename">文件名</param>
        /// <returns></returns>
        public static string TestFile(int time, string filepath, string filename)
        {
            string path = $"{filepath}{filename}";
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Restart();
            while (true)
            {
                if (stopwatch.ElapsedMilliseconds >= time) return null;
                if (File.Exists(path))
                {
                    string str = File.ReadAllText(path, Encoding.UTF8);
                    File.Delete(path);
                    return str;
                }
            }
        }
    }
}
